﻿@model Contrib.Taxonomies.ViewModels.TaxonomyFieldViewModel
@using Contrib.Taxonomies.Helpers;
@using Orchard.Utility.Extensions;
@{
    Style.Include("admin-taxonomy.css");
    Style.Include("ExtraTax.css");
    Script.Include("~/Themes/TheAdmin/scripts/admin.js").AtFoot();
    Script.Include("~/Modules/Contrib.Taxonomies/Scripts/admin-taxonomy-expando.js").AtFoot();
    //Script.Include("admin-taxonomy-expando.js");
    Script.Require("jQuery");
    int termIndex = 0, ti;

    var name = "list-" + Model.Name;
    var theId = "#" + name;
    var filter = "filter" + Model.Name;
    var fId = "#" + filter;
}
<fieldset>
    <legend>@Model.Name.CamelFriendly()</legend>
    <div class="expando">
        <div class="filter-box"><input placeholder="Filter.." type="text" id=@filter class="filter-input" /></div>
        @if (!String.IsNullOrWhiteSpace(Model.Settings.Hint))
        {
            <span class="hint">@Model.Settings.Hint</span>
        }
        <ul id=@name>
            @foreach (var entry in Model.Terms)
            {
                ti = termIndex;
                <li>@* Tabs for levels *@
                    @for (var i = 1; i <= entry.GetLevels(); i++)
                    { <span class="gap">&nbsp;</span> }
                    @{
                    var disabled = !entry.Selectable || (Model.Settings.LeavesOnly && Model.Terms.Any(t => t.Path.Contains(entry.Path + entry.Id)));
                    if (Model.Settings.SingleChoice)
                    {
                        <input @if (disabled)
                               { <text>disabled="disabled"</text> } type="radio" value="@Model.Terms[ti].Id" @if (entry.Id == Model.SingleTermId)
                                                                                                             { <text>checked="checked"</text> } name="@Html.FieldNameFor(m => m.SingleTermId)" id="@Html.FieldIdFor(m => m.Terms[ti].IsChecked)"/> 
                    }
                    else
                    {            
                        <input @if (disabled)
                               { <text>disabled="disabled"</text> } type="checkbox" value="true" @if (entry.IsChecked)
                                                                                                 { <text>checked="checked"</text> } name="@Html.FieldNameFor(m => m.Terms[ti].IsChecked)" id="@Html.FieldIdFor(m => m.Terms[ti].IsChecked)"/> 
                    }
                    }
                    @Html.HiddenFor(m => m.Terms[ti].Id)
                    <label class="forcheckbox" for="@Html.FieldIdFor(m => m.Terms[ti].IsChecked)">@entry.Name</label>
                </li>
                    termIndex++;
            }
        </ul>
    </div>
</fieldset>
@using (Script.Foot())
{
    <script>
        (function ($) {
            jQuery.expr[':'].Contains = function (a, i, m) {
                return (a.textContent || a.innerText || "").toUpperCase().indexOf(m[3].toUpperCase()) >= 0;
            };


            function listFilter(header, list) {
                var input = $('@fId');

                input
      .change(function () {
          var filter = $(this).val();
          if (filter) {
              $(list).find("label:not(:Contains(" + filter + "))").parent().slideUp();
              $(list).find("label:Contains(" + filter + ")").parent().slideDown();
          } else {
              $(list).find("li").slideDown();
          }
          return false;
      })
        .keyup(function () {
            $(this).change();
        });
            }

            $(function () {
                listFilter($("#header"), $('@theId'));
            });
        } (jQuery));

//        $(document).ready(function () {
//            $('@fId').wrap('<span class="deleteicon" />').after($('<span/>').click(function () {
//                $(this).prev('input').val('').focus();
//            }));
//        });

    </script>
}